<!doctype html>
<html lang="en">
<head><meta name="generator" content="Hexo 3.9.0">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>言成言成啊</title>
    <link rel="shortcut icon" href="/images/favicon.ico">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/css/meethigher-font.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/css/animate.css">
    <link rel="stylesheet" href="/css/index.css">
</head>
<body>
<header class="header">
    <div class="header-menu"><span class="fa fa-bars"></span></div>
    <div class="header-search"><span class="fa fa-search"></span></div>
    <div class="header-title"><a href="/">言成言成啊 | <span class="header-subtitle">Kit Chen&#39;s Blog</span></a></div>
    <ul class="header-navbar clearFix">
        
            <li><a href="/">主页</a></li>
        
            <li><a href="/tags">标签</a></li>
        
            <li><a href="/archives">归档</a></li>
        
            <li><a href="/search">导航</a></li>
        
    </ul>
</header>
<main class="main">
    <div class="main-content meethigher-scroll">
    <article class="post">
        <div class="post-title">
            <h2 class="title">浅谈static关键字</h2>
        </div>
        <div class="post-media2">
            <span class="post-time">2019-09-16</span>
            
                <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/java/">java</a></li></ul>
            
        </div>
        <div class="post-content blog-markdown">
            <h3 id="浅谈static关键字"><a href="#浅谈static关键字" class="headerlink" title="浅谈static关键字"></a>浅谈static关键字</h3><p>如果一个<strong>成员变量</strong>使用了static关键字，则该成员变量不再属于对象自己，而是<strong>属于类</strong>。</p>
<p>如果一个<strong>成员方法</strong>使用了static关键字，则该成员方法不再属于对象自己，而是<strong>属于类</strong>。</p>
<p>如果没有static关键字，则必须创建对象，然后才能通过对象使用它。</p>
<p>如果有了static关键字，则不需要创建对象，直接就能通过<strong>类名称</strong>来访问</p>
<p>无论是成员变量，还是成员方法，<strong>如果有了static，都推荐使用类名称来调用</strong></p>
<p>静态变量：类名称.静态变量</p>
<p>静态方法：类名称.静态方法()</p>
<figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line">package demo03;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title">MyClass</span> &#123;</span><br><span class="line">	String name;</span><br><span class="line">	<span class="keyword">static</span> <span class="keyword">int</span> age;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">method</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">		System.<span class="keyword">out</span>.println(<span class="string">"这是一个成员方法"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">methodStatic</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">		System.<span class="keyword">out</span>.println(<span class="string">"这是一个静态方法"</span>);</span><br><span class="line">		<span class="comment">//System.out.println(name);//报错，静态方法中不能访问非静态变量</span></span><br><span class="line">		System.<span class="keyword">out</span>.println(age);<span class="comment">//静态方法中可以访问静态变量</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">Demo03StaticMethod</span> &#123;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/*</span></span><br><span class="line"><span class="comment">	 * 一旦使用了static修饰成员方法，那么这就成为了静态方法。静态方法不属于对象，而是属于类 </span></span><br><span class="line"><span class="comment">	 * 如果没有static关键字，那么必须创建对象，然后才能通过对象使用他</span></span><br><span class="line"><span class="comment">	 * 如果有了static关键字，那么不需要创建对象，直接就能通过类名称来访问他</span></span><br><span class="line"><span class="comment">	 * </span></span><br><span class="line"><span class="comment">	 * 无论是成员变量，还是成员方法，如果有了static,都推荐使用类名称进行调用</span></span><br><span class="line"><span class="comment">	 * 静态变量：类名称.静态变量</span></span><br><span class="line"><span class="comment">	 * 静态方法：类名称.静态方法()</span></span><br><span class="line"><span class="comment">	 * </span></span><br><span class="line"><span class="comment">	 * </span></span><br><span class="line"><span class="comment">	 *注意事项：</span></span><br><span class="line"><span class="comment">	 *1.静态不能直接访问非静态</span></span><br><span class="line"><span class="comment">	 *原因：因为在内存当中是`先`有静态的内容，`后`有非静态的内容</span></span><br><span class="line"><span class="comment">	 *"先人不知道后人，但是后人知道先人"</span></span><br><span class="line"><span class="comment">	 *2.静态方法中不能使用this</span></span><br><span class="line"><span class="comment">	 *原因：this代表当前对象，通过谁调用的方法，谁就是当前的对象</span></span><br><span class="line"><span class="comment">	 * </span></span><br><span class="line"><span class="comment">	 * </span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span>(<span class="params">String[] args</span>)</span> &#123;</span><br><span class="line">		<span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line">		MyClass obj=<span class="keyword">new</span> MyClass();</span><br><span class="line">		obj.method();</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//对于本类当中的静态方法，前面的类名称可以省略</span></span><br><span class="line">		<span class="comment">//test.haha();//如果没有使用static关键字，则会报错</span></span><br><span class="line">		haha1();</span><br><span class="line">		Demo03StaticMethod.haha1();<span class="comment">//这两个是完全等效</span></span><br><span class="line">		</span><br><span class="line">		<span class="comment">//对于静态方法来书，可以通过对象名进行调用，也可以直接通过类名进行调用</span></span><br><span class="line">		obj.methodStatic();<span class="comment">//正确，不推荐。这种方法在编译之后也会被javac翻译成"类名称.静态方法名"</span></span><br><span class="line">		MyClass.methodStatic();<span class="comment">//正确，推荐</span></span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">haha</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">		System.<span class="keyword">out</span>.println(<span class="string">"haha"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">haha1</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">		System.<span class="keyword">out</span>.println(<span class="string">"haha1"</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>注意事项:</p>
</blockquote>
<ul>
<li>静态不能直接访问非静态。<ul>
<li>原因：因为在内存当中是<code>先</code>有静态的内容，<code>后</code>有非静态的内容</li>
<li><code>先人不知道后人，但是后人知道先人</code></li>
</ul>
</li>
<li>静态方法中不能使用this<ul>
<li>原因：this 代表当前对象，通过谁调用的方法，谁就是当前的对象</li>
</ul>
</li>
</ul>
<h3 id="浅谈静态代码块"><a href="#浅谈静态代码块" class="headerlink" title="浅谈静态代码块"></a>浅谈静态代码块</h3><p>静态代码块：</p>
<p>public class 类名称 {</p>
<p>​        static {</p>
<p>​               // 静态代码块的内容</p>
<p>​        }</p>
<p>}</p>
<p>特点：当第一次用到本类时，静态代码执行唯一的一次</p>
<p>静态内容总是优先于非静态，所以静态代码块比构造方法先执行</p>
<p>静态代码块的典型用途：</p>
<p>用来一次性地对静态成员变量进行赋值</p>
<figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">package demo03;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">静态代码块：</span></span><br><span class="line"><span class="comment">public class 类名称 &#123;</span></span><br><span class="line"><span class="comment">  static &#123;</span></span><br><span class="line"><span class="comment">     // 静态代码块的内容</span></span><br><span class="line"><span class="comment">  &#125;</span></span><br><span class="line"><span class="comment">&#125;</span></span><br><span class="line"><span class="comment">特点：当第一次用到本类时，静态代码执行唯一的一次</span></span><br><span class="line"><span class="comment">静态内容总是优先于非静态，所以静态代码块比构造方法先执行</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">静态代码块的典型用途：</span></span><br><span class="line"><span class="comment">用来一次性地对静态成员变量进行赋值</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">class</span> <span class="title">Student</span> &#123;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">Student</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">		System.<span class="keyword">out</span>.println(<span class="string">"构造函数执行"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">static</span> &#123;</span><br><span class="line">		System.<span class="keyword">out</span>.println(<span class="string">"静态代码执行唯一一次"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">Demo03Static</span> &#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span>(<span class="params">String[] args</span>)</span> &#123;</span><br><span class="line">		Student s=<span class="keyword">new</span> Student();</span><br><span class="line">		Student s1=<span class="keyword">new</span> Student();</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//经过上述测试，静态代码块，只执行一次</span></span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
        </div>
        
            <div class="busuanzi">
                <span id="busuanzi_container_page_pv">阅读量<span id="busuanzi_value_page_pv"></span>次</span>
                <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
            </div>
        
    </article>
</div>
    <pointer href="javascript:void(0);" class="arrow fa fa-arrow-up"></pointer>
<pointer href="javascript:void(0);" class="arrow fa fa-arrow-down"></pointer>

</main>
<aside class="aside">
    <div class="aside-content">
        <div class="aside-avatar"><img src="/images/kitchen.jpg" alt="言成"></div>
        <div class="aside-author">言成</div>
        <div class="aside-description">爱学习，爱运动，爱看妹子</div>
        <ul class="aside-contact clearFix">
            <li><span class="fa fa-github"></span><a href="https://github.com/meethigher">Github</a></li>
            <li><span class="fa fa-envelope"></span><a href="javascript:void(0);">meethigher@qq.com</a></li>
            <li><span class="fa fa-weixin"></span><a href="javascript:void(0);"><img src="/images/weixin.png" alt="扫描微信二维码，加我哦！"></a></li>
        </ul>
    </div>
</aside>
<div class="toolbar"><span class="fa fa-th"></span></div>
<div id="particles-js"></div>
<script src="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/js/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/js/particles.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/js/app.js"></script>
<script src="/js/meethigher.js"></script>
<script src="/js/toolbar.js"></script>
</body>
</html>